package com.example.aop.proxy;

import java.lang.reflect.Proxy;

public class Main {
    public static void main(String[] args) {
        //静态代理
//        HouseProxy proxy = new HouseProxy(new RealHouseSubject());
//        proxy.rentHouse();
//        System.out.println("========================");
//        HouseSubject houseSubject = new RealHouseSubject();
//        houseSubject.rentHouse();

        //JDK 动态代理
        /**
         * newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h)
         * loader: 加载代理类的classload
         * interfaces: 要实现的接口
         * h: 代理要做的事情, 需要实现InvocationHandler 这个接口
         */

        RealHouseSubject target = new RealHouseSubject();
        HouseSubject proxy = (HouseSubject) Proxy.newProxyInstance(target.getClass().getClassLoader(),
                new Class[]{HouseSubject.class}, new JDKInvocationHandler(target));
        proxy.rentHouse();
        proxy.saleHouse();

    }
}
